﻿using FluentValidation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using PMS.Data.Entities.Settings;
using PMS.Web.Framework.Validators;
using PMS.Web.Models.Customers;
using System;

namespace PMS.Web.Validators.Customer
{
    public partial class CustomerModelValidator : BaseModelValidator<CustomerModel>
    {
        public CustomerModelValidator(CustomerSettings customerSettings)
        {
            //Validate when Edit Customer(CustomerId == 0)
            RuleFor(x => x.Email).NotEmpty().When(x => x.Id == 0).WithMessage(ValidatorRuleString.EmailRequired);
            RuleFor(x => x.Email).EmailAddress().When(x => x.Id == 0).WithMessage(ValidatorRuleString.Validators_Email);
            RuleFor(x => x.LoginName).IsUsername(customerSettings).When(x => x.Id == 0).WithMessage(customerSettings.UsernameValidationTips);
            RuleFor(x => x.Mobile).IsMobileNumber(customerSettings).When(x => x.Id == 0).WithMessage(ValidatorRuleString.Validators_MobilePhone);

            //Password rule when Password Not Empty
            ((IRuleBuilderOptions<CustomerModel, string>)RuleFor(x => x.Password).IsPassword(customerSettings)).When(x => x.Password != null && x.Password.Length > 0);
        }

        protected override bool PreValidate(ValidationContext<CustomerModel> context, ValidationResult result)
        {
            var httpContextAccessor = context.GetServiceProvider().GetService(typeof(IActionContextAccessor));
            var actionDescriptor = ((IActionContextAccessor)httpContextAccessor).ActionContext.ActionDescriptor as ControllerActionDescriptor;
            var actionName = actionDescriptor?.ActionName;
            var controllerName = actionDescriptor?.ControllerName;
            //https://docs.fluentvalidation.net/en/latest/advanced.html 
            if (controllerName.Equals("CustomerWebApi", StringComparison.CurrentCultureIgnoreCase) && actionName.Equals("CustomerAssginRoleApi", StringComparison.CurrentCultureIgnoreCase))
                return false;
            else if (controllerName.Equals("CustomerWebApi", StringComparison.CurrentCultureIgnoreCase) && actionName.Equals("SendCustomEmailToCustomerApi", StringComparison.CurrentCultureIgnoreCase))
            {
                RuleFor(m => m.Email).NotEmpty().WithMessage(ValidatorRuleString.EmailRequired);
                //Validate Email when email info not hide like a****@example.com
                RuleFor(x => x.Email).EmailAddress().When(x => !x.Email.Contains('*')).WithMessage(ValidatorRuleString.Validators_Email);
                RuleFor(m => m.NickName).NotEmpty().WithMessage(string.Format(ValidatorRuleString.FieldRequired, "收信人称谓"));
                RuleFor(m => m.SendEmail.Subject).NotEmpty().WithMessage(string.Format(ValidatorRuleString.FieldRequired, "邮件主题(标题)"));
                RuleFor(m => m.SendEmail.Body).NotEmpty().WithMessage(string.Format(ValidatorRuleString.FieldRequired, "邮件内容"));
                RuleFor(m => m.Id).Must(x => x > 0).WithMessage(ValidatorRuleString.RequireEmailBindtoAccountWhenSendEmail);
               
                return false;
            }
            return true;
        }
    }
}
